package com.workManager.service;

import com.workManager.mapper.TbProcurementEstimateMapper;
import com.workManager.mapper.TbProcurementMapper;
import com.workManager.pojo.TbProcurement;
import com.workManager.pojo.TbProcurementEstimate;
import com.workManager.util.DateUtil;
import com.workManager.util.WebUtil;
import com.workManager.vo.ProcurementEstimateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by stran on 2017/11/1 0001.
 */
@Service
public class ProcurementEstimateServiceImpl implements ProcurementEstimateService{

    @Autowired
    private TbProcurementEstimateMapper procurementEstimateMapper;

    @Autowired
    private TbProcurementMapper procurementMapper;

    public List<ProcurementEstimateVo> query(Map map) {
        return procurementEstimateMapper.query(map);
    }

    public void add(TbProcurementEstimate procurementEstimate) {
        if(null!=procurementEstimate.getRemark()&&!"".equals(procurementEstimate.getRemark())){
            procurementEstimate.setRemark("["+ DateUtil.getDate()+":"+ WebUtil.getCurrentUserVO().getUserName()+"]:"+procurementEstimate.getRemark());
        }
        procurementEstimateMapper.add(procurementEstimate);
    }

    public void makeInvoice(TbProcurementEstimate procurementEstimate) {
        Date date = new Date();
        TbProcurement procurement = new TbProcurement();
        procurement.setAmount(procurementEstimate.getAmount());
        procurement.setArrivalDate(procurementEstimate.getArrivalDate());
        procurement.setCreateDate(date);
        String remark = "";
        if(null!=procurementEstimate.getRemark()&&!"".equals(procurementEstimate.getRemark())) {
            String remarkArr[] = procurementEstimate.getRemark().split("\\|");
            for (int i = 0; i < remarkArr.length; i++) {
                if (i == remarkArr.length - 1) {
                    remark += "[" + DateUtil.getDate() + ":" + WebUtil.getCurrentUserVO().getUserName() + "]:" + remarkArr[i];
                } else {
                    remark += remarkArr[i] + "|";
                }
            }
        }
        procurement.setDescription(remark);
        procurement.setInterest(procurementEstimate.getInterest());
        procurement.setInterestRate(procurementEstimate.getInterestRate());
        procurement.setIsValid(true);
        procurement.setPaymentDate(procurementEstimate.getPaymentDate());
        procurement.setProcurementCost(procurementEstimate.getProcurementCost());
        procurement.setProcurementType(procurementEstimate.getProcurementType());
        procurement.setProjectId(procurementEstimate.getProjectId());
        procurement.setPurchaseDate(procurementEstimate.getPurchaseDate());
        procurement.setVat(procurementEstimate.getVat());
        procurementMapper.insert(procurement);

        Map map = new HashMap();
        map.put("procurementId", procurement.getProcurementId());
        map.put("remark", remark);
        map.put("id", procurementEstimate.getId());
        procurementEstimateMapper.makeInvoice(map);
    }

    public void invalid(TbProcurementEstimate procurementEstimate) {
        Map map = new HashMap();
        String remarkArr[] = procurementEstimate.getRemark().split("\\|");
        String remark = "";
        for(int i=0;i<remarkArr.length;i++){
            if(i==remarkArr.length-1){
                remark += "["+ DateUtil.getDate()+":"+ WebUtil.getCurrentUserVO().getUserName()+"]:"+remarkArr[i];
            }else{
                remark += remarkArr[i]+"|";
            }
        }
        map.put("remark", remark);
        map.put("id", procurementEstimate.getId());
        procurementEstimateMapper.invalid(map);
    }
}
